package carsharing;

import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import aima.search.framework.Problem;
import aima.search.framework.Search;
import aima.search.framework.SearchAgent;
import aima.search.informed.HillClimbingSearch;
import aima.search.informed.SimulatedAnnealingSearch;


public class testMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Carsharing c = new Carsharing(80,40);
		System.out.println("Distancia solucion inicial: " + c.solIni2());
		//c.printMembers();
		System.out.println();
		long ini1, fi1, time;
		
		ini1 = System.currentTimeMillis();
		//SimulatedAnnealingSearchH2(c);
		HillClimbingSearchH1(c);
		fi1 = System.currentTimeMillis();
		//ini2 = System.currentTimeMillis();
		//HillClimbingSearchH1(c);
		//fi2 = System.currentTimeMillis();
		time = fi1 - ini1;
		//System.out.println("Temps d'execucio SA + (200,100) + solIni2 + H1 + COp2 + #iter = 2000 + pasos = 10 + k = 1000 + lambda = 0.001 -->" + time + " milisegons // " + time/1000 + " segons // " + (time/1000)/60 + " minuts");
		System.out.println("Temps d'execucio HC + (200,100) + solIni2 + H1 + COp2 -->" + time + " milisegons // " + time/1000 + " segons // " + (time/1000)/60 + " minuts");
		//time = fi2 - ini2;
		//System.out.println("Temps d'execucio H2 = " + time + " milisegons // " + time/1000 + " segons // " + (time/1000)/60 + " minuts");
		//HillClimbingSearchH2(c);
		//SimulatedAnnealingSearch(c);
	}
	
	
    private static void HillClimbingSearchH1(Carsharing c) {

        System.out.println("Hill Climbing Heuristic 1...");

        try {
            Problem problem = new Problem(c, new CarsharingSuccessors(), new CarsharingGoalTest(), new CarsharingHeuristic1());
            Search search = new HillClimbingSearch();
            SearchAgent agent = new SearchAgent(problem,search);

            //System.out.println();

           printActions(agent.getActions());
           printInstrumentation(agent.getInstrumentation());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }
    private static void HillClimbingSearchH2(Carsharing c) {

        System.out.println("Hill Climbing Heuristic 2...");

        try {
            Problem problem = new Problem(c, new CarsharingSuccessors(), new CarsharingGoalTest(), new CarsharingHeuristic2());
            Search search = new HillClimbingSearch();
            SearchAgent agent = new SearchAgent(problem,search);

            //System.out.println();

           printActions(agent.getActions());
           printInstrumentation(agent.getInstrumentation());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }
    
    private static void SimulatedAnnealingSearchH1(Carsharing c) {

        System.out.println("Simulated Annealing H1...");

        try {

            Problem problem =  new Problem(c, new CarsharingSuccessors(), new CarsharingGoalTest(), new CarsharingHeuristic1());
            Search search =  new SimulatedAnnealingSearch(10000, 5, 10, 0.01);	//steps, stiter(lim), k, lamb
            SearchAgent agent = new SearchAgent(problem,search);


           printActions(agent.getActions());
           printInstrumentation(agent.getInstrumentation());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }
    private static void SimulatedAnnealingSearchH2(Carsharing c) {

        System.out.println("Simulated Annealing H2...");

        try {

            Problem problem =  new Problem(c, new CarsharingSuccessors(), new CarsharingGoalTest(), new CarsharingHeuristic2());
            Search search = new SimulatedAnnealingSearch(10000, 5, 10, 0.01);	//2000,100,5,0.001 steps, stiter(lim), k, lamb
            SearchAgent agent = new SearchAgent(problem,search);

            //System.out.println();

           printActions(agent.getActions());
           printInstrumentation(agent.getInstrumentation());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }
    
    
    

    private static void printInstrumentation(Properties properties) {
        Iterator keys = properties.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            String property = properties.getProperty(key);
            System.out.println(key + " : " + property);
        }

    }

    private static void printActions(List actions) {
        for (int i = 0; i < actions.size(); i++) {
            String action = (String) actions.get(i);
            System.out.println(action);
        }
    }



}
